//
//  CustomSheetView.m
//  jingjing2.0
//
//  Created by apple on 16/5/28.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "CustomSheetView.h"

@implementation CustomSheetView

+(void)sharedSheetWithPhotoBlock:(void(^)())photoblock andPictureBlock:(void(^)())pictureblock andCancleBlock:(void(^)())cancleblock
{
    UIWindow * mainWindow = [UIApplication sharedApplication].keyWindow;
    UIView * backView = [[UIView alloc]initWithFrame:mainWindow.bounds];
    backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    backView.userInteractionEnabled = YES;
    [mainWindow addSubview:backView];

    CGRect bounds = [UIScreen mainScreen].bounds;
    CustomSheetView * alertView = [[[self class] alloc]initWithFrame:CGRectMake(0,bounds.size.height,bounds.size.width, 145) WithFirstTitles:@[@"拍照",@"从相册中选取",@"取消"]];
    alertView.tag = 111;
    alertView.photoBlock = photoblock;
    alertView.cancleBlock = cancleblock;
    alertView.pictureBlock = pictureblock;
    [backView addSubview:alertView];
    
    [UIView animateWithDuration:0.25 animations:^{
        alertView.frame = CGRectMake(0,bounds.size.height-145,bounds.size.width, 145);
    }];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:alertView action:@selector(tapGestureClick:)];
    [backView addGestureRecognizer:tap];
    
    
}
-(id)initWithFrame:(CGRect)frame WithFirstTitles:(NSArray *)titles
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor whiteColor];
       UIButton * photo = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, 48)];
        [photo setTitle:titles[0] forState:UIControlStateNormal];
        photo.tag = 11;
        photo.titleLabel.font = [UIFont systemFontOfSize:14];
        [photo setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [photo addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
       UIView * lineOne = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(photo.frame), self.frame.size.width, 0.5)];
        lineOne.backgroundColor = [UIColor colorWithRed:189/255.0 green:189/255.0 blue:189/255.0 alpha:1];

        UIButton * picture = [[UIButton alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(lineOne.frame), frame.size.width, 48)];
        [picture setTitle:titles[1] forState:UIControlStateNormal];
        picture.tag = 12;
        picture.titleLabel.font = [UIFont systemFontOfSize:14];
        [picture setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [picture addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
       UIView * lineViewTwo = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(picture.frame), self.frame.size.width,0.5)];
        lineViewTwo.backgroundColor = [UIColor colorWithRed:189/255.0 green:189/255.0 blue:189/255.0 alpha:1];
        
        UIButton * cancle = [[UIButton alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(lineViewTwo.frame), frame.size.width, 48)];
        cancle.tag = 13;
        cancle.titleLabel.font = [UIFont systemFontOfSize:14];
        [cancle setTitle:titles[2] forState:UIControlStateNormal];
        [cancle setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [cancle addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:photo];
        [self addSubview:picture];
        [self addSubview:lineOne];
        [self addSubview:cancle];
        [self addSubview:lineViewTwo];
    }
    return self;
}
-(void)buttonClick:(UIButton *)sender
{
    if (sender.tag == 11)
    {
        if (_photoBlock)
        {
            _photoBlock();
        }
    }else if (sender.tag == 12)
    {
        if (_pictureBlock)
        {
            _pictureBlock();
        }
    }else
    {
        if (_cancleBlock)
        {
            _cancleBlock();
        }
    }
    [self removeWithAnimationWithUIView:self.superview];
    
}
-(void)tapGestureClick:(UIGestureRecognizer *)gesture
{
    UIView * view = gesture.view;
    CustomSheetView * custom = (CustomSheetView *)[view viewWithTag:111];
    [self removeWithAnimationWithUIView:custom];
}
-(void)removeWithAnimationWithUIView:(UIView *)view
{
    CGRect bounds = [UIScreen mainScreen].bounds;
    
    [UIView animateWithDuration:0.25 animations:^{
        view.frame = CGRectMake(0,bounds.size.height,bounds.size.width, 121);
    } completion:^(BOOL finished) {
        [self.superview removeFromSuperview];
    }];
}

@end
